//
//  HomeViewController.m
//  MiWei
//
//  Created by 刘星 on 2017/10/13.
//  Copyright © 2017年 miwei. All rights reserved.
//

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#import "HomeViewController.h"
#import "ReactiveCocoa.h"
#import "HomeViewModel.h"
#import "HomeTableView.h"
#import "HomeTableHeadView.h"
#import "Base.h"

@interface HomeViewController ()

@property (nonatomic, strong) HomeViewModel *viewModel;
@property (nonatomic, strong) HomeTableView *homeTableView;
@property (nonatomic, strong) HomeTableHeadView *headView;

@end

@implementation HomeViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configureView];
    [self bindModel];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)configureView {
    self.view.backgroundColor = UIColorFromRGB(0x292A2E);
    [self.view addSubview:self.homeTableView];
//    self.homeTableView.tableHeaderView = self.headView;
}

- (void)bindModel {
    @weakify(self);
    RAC(self.homeTableView, hotRecomendListArray) = RACObserve(self.viewModel, recommendDataArray); // 热门推荐
    
    RAC(self.headView, weekRecomendArray) = RACObserve(self.viewModel, weekRecommendDataArray);  //
//    RAC(self.headView, adArray) = RACObserve(self.viewModel, adArray); // banner
//    RAC(self.headView, adsModelArray) = RACObserve(self.viewModel, adModelArray); // 新闻
    
    [self.viewModel.loadDataCommand execute:self.homeTableView];
}

- (HomeTableView *)homeTableView {
    if (_homeTableView == nil) {
        _homeTableView = [[HomeTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _homeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _homeTableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
        _homeTableView.backgroundColor = UIColorFromRGB(0x292A2E);
        _homeTableView.homeViewModel = self.viewModel;
    }
    return _homeTableView;
}

- (HomeTableHeadView *)headView {
    @weakify(self);
    if (_headView == nil) {
        _headView = [[HomeTableHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, (kScreenHeight* 450)/ 750 + 285)];
        _headView.homeViewModel = self.viewModel;
        _headView.tableView = self.homeTableView;
    }
    return _headView;
}

@end
